package com.bc004346.travelus.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.bc004346.travelus.R;
import com.bc004346.travelus.SettingsActivity;
import com.bc004346.travelus.TripMeManualStartActivity;
import com.bc004346.travelus.utility.Utility;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by Renats on 12/8/2014.
 */
public class TripMeDestinationsFragment extends ListFragment {

    Set<String> mDestinations = new HashSet<>();

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        @SuppressLint("InflateParams")
        View v = inflater.inflate(R.layout.fragment_trip_me_destinations, null);

        try {
            final EditText aaa = (EditText) v.findViewById(R.id.fragment_trip_me_destinations_aaa);
            Button add = (Button) v.findViewById(R.id.fragment_trip_me_destinations_add);
            Button next = (Button) v.findViewById(R.id.fragment_trip_me_destinations_next);

            add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (aaa.getText() == null || aaa.getText().toString().length() != 3) {
                        Toast.makeText(getActivity(), "Invalid AAA", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    mDestinations.add(aaa.getText().toString());
                    SettingsActivity.setFavoriteDestinations(getActivity().getApplicationContext(), mDestinations);
                    updateList();
                    aaa.setText("");
                }
            });
            next.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Utility.onStop(getActivity());
                    startActivity(new Intent(getActivity(), TripMeManualStartActivity.class));
                    getActivity().finish();
                }
            });

            updateList();

        } catch (Exception e) {
            Utility.log(e, getActivity());
        }

        return v;
    }

    private void updateList() {

        mDestinations.addAll(SettingsActivity.getFavoriteDestinations(getActivity()));
        ArrayList<String> list = new ArrayList<>();
        list.addAll(mDestinations);
        ArrayAdapter<String> favDestinations =
                new ArrayAdapter<>(getActivity(),
                        android.R.layout.simple_list_item_1,
                        list);
        setListAdapter(favDestinations);
    }
}
